import { RoutesAlias } from '@/router/routesAlias'
import type { NavMenuItem } from '@/types/config'

class NavService {
  private readonly navConfig: NavMenuItem[]

  /**
   * 导航服务
   */
  constructor() {
    this.navConfig = [
      {
        label: 'navbar.home',
        path: RoutesAlias.Home,
        children: [
          { label: 'navbar.category', path: '/category' },
          { label: 'navbar.tag', path: '/tag' }
        ]
      },
      {
        label: 'navbar.essay',
        path: RoutesAlias.Essay,
        children: [{ label: 'navbar.archive', path: '/archive' }]
      },
      { label: 'navbar.record', path: RoutesAlias.Record },
      { label: 'navbar.album', path: RoutesAlias.Album },
      { label: 'navbar.message', path: RoutesAlias.Message },
      { label: 'navbar.about', path: RoutesAlias.About }
    ]
  }

  /**
   * 获取导航配置
   */
  getNavConfig(): NavMenuItem[] {
    return this.navConfig
  }

  /**
   * 根据路径获取导航项
   * @param path
   */
  getNavByPath(path: string): NavMenuItem | undefined {
    return this.navConfig.find((item) => item.path === path)
  }

  /**
   * 获取所有路径
   */
  getAllPaths(): string[] {
    const paths: string[] = []
    const collectPaths = (items: NavMenuItem[]) => {
      items.forEach((item) => {
        paths.push(item.path)
        if (item.children) {
          collectPaths(item.children)
        }
      })
    }
    collectPaths(this.navConfig)
    return paths
  }

  /**
   * 判断当前路径是否激活
   * @param currentPath
   * @param targetPath
   */
  isActive(currentPath: string, targetPath: string): boolean {
    return currentPath === targetPath || currentPath.startsWith(targetPath + '/')
  }
}

/**
 * 导航服务
 */
export const navService = new NavService()

export default navService
